package com.zwy.Shopping.servlet_;

import com.zwy.Shopping.dao.UserDao;
import com.zwy.Shopping.dao.impl.UserDaoImpl;
import com.zwy.Shopping.model.User;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/modify_user_info")
public class ModifyUserInfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname = request.getParameter("username");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");
        String email = request.getParameter("email");
        User user = new User();
        user.setName(uname);
        user.setMobile(mobile);
        user.setAddress(address);
        user.setEmail(email);
        UserDao userDao = new UserDaoImpl();
        boolean success = userDao.changeUserInfo(user);
        if (success){
            System.out.println("修改成功");
            //将session里的user更新
            request.getSession().setAttribute("user",user);
            request.setAttribute("message","修改成功");
            request.setAttribute("page","index.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }else {
            System.out.println("修改失败");
            request.setAttribute("message","修改失败");
            request.setAttribute("page","userInfo.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }

    }
}
